package com.canbot.u05.activity.base

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.free.MediaInfo
import com.canbot.u05.utils.Logger
import com.canbot.u05.utils.PathConst
import com.canbot.u05.utils.SoundManager
import com.canbot.u05.utils.ToastTools
import com.canbot.u05.view.GuideRecordDialog
import kotlinx.android.synthetic.main.base_custom_audio_actiivty.*

/**
 * Created by zmp on 2019/6/4 10:58
 * @author zmp
 * 空闲状态待机点选择界面
 */
class BaseCustomAudioActivity : BaseActivityNoTitle(), View.OnClickListener {

    private var guideRecordDialog: GuideRecordDialog? = null

    private var toastTools: ToastTools? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.base_custom_audio_actiivty)
        initViews()

    }

    private fun initViews() {
        back_btn_head.setOnClickListener(this)
        customAudioTextView.setOnClickListener(this)
        customTTSTextView.setOnClickListener(this)
        customRecordTextView.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.back_btn_head -> {
                finish()
            }
            R.id.customAudioTextView -> {
                startActivityForResult(Intent(this, BaseAudioListActivity::class.java), AUDIO_CODE)
            }
            R.id.customTTSTextView -> {
                startActivityForResult(Intent(this, BaseChooseTtsActivity::class.java), AUDIO_CODE)
            }
            R.id.customRecordTextView -> {
                record()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == AUDIO_CODE) {
            setResult(Activity.RESULT_OK, data)
            finish()
        }
    }

    /**
     * 进入录音
     */
    private fun record() {
        if (guideRecordDialog == null) {
            //点击语音录入图标
            guideRecordDialog = GuideRecordDialog(this, object : GuideRecordDialog.IRecodeListener {
                override fun finish(fileName: String) {
                    Logger.e("test", "fileName$fileName")
                    if (TextUtils.isEmpty(fileName)) {
                        toastTools?.showText(getString(R.string.recoding_null))
                        return
                    }
                    Logger.d("SpeechSetupActivity", "录音的文件名：$fileName")
                    SoundManager.getInstance().playSound(PathConst.ROOT_PATH + PathConst.RECORD_FILE_PATH + fileName, null)
                    setAudioResult(fileName)
                }

                override fun cancel() {

                }

                override fun error() {
                    toastTools?.showText(getString(R.string.recoding_error))
                }
            })
        }
        guideRecordDialog?.show()

    }

    private fun setAudioResult(fileName: String) {
        setResult(Activity.RESULT_OK, Intent()
                .putExtra(BaseVoiceSetActivity.AUDIO_TYPE_KEY, MediaInfo.AUDIO_TYPE_RECODE)
                .putExtra(BaseVoiceSetActivity.AUDIO_PATH_OR_CONTENT, PathConst.RECORD_FILE_PATH + fileName)
                .putExtra(BaseVoiceSetActivity.AUDIO_SIMPLE_NAME_KEY, fileName))
        finish()
    }

    companion object {
        const val AUDIO_CODE = 1001
    }
}